package com.bestv.search.common.dao.hibernate;

import com.bestv.search.common.dao.NetInfoReviewDao;
import com.bestv.search.common.model.NetInfoReview;
import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.transform.Transformers;

import java.util.List;
import java.util.Map;

public class NetInfoReviewDaoHibernate extends GenericDaoHibernate<NetInfoReview, Long> implements NetInfoReviewDao {

    @Override
    public NetInfoReview getByReviewId(String reviewId, String source) {
        String hql = "from NetInfoReview where reviewId = ? and source= ?";
        List<?> list = getHibernateTemplate().find(hql, new String[]{reviewId, source});
        return list == null || list.size() == 0 ? null : (NetInfoReview) list.get(0);
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<NetInfoReview> getNetInfoReviewsByNetInfoId(long netInfo_id, int start, int limit) {
        String sql = "select c.author, c.content, c.published, c.totalReviewed, c.usefulReviewed"
                + "    from netInfoReview as c inner join netInfo"
                + " on c.netId = netInfo.netId where netInfo.id=? order by usefulReviewed desc";

        Session session = null;
        List<NetInfoReview> list = null;
        try {
            session = getSession();
            Query query = session.createSQLQuery(sql).addScalar("author", Hibernate.STRING)
                    .addScalar("content", Hibernate.BLOB).addScalar("published", Hibernate.STRING)
                    .addScalar("totalReviewed", Hibernate.INTEGER).addScalar("usefulReviewed", Hibernate.INTEGER)
                    .setResultTransformer(Transformers.aliasToBean(NetInfoReview.class));
            query.setLong(0, netInfo_id);
            query.setFirstResult(start);
            query.setMaxResults(limit);
            list = query.list();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null)
                releaseSession(session);
        }

        return list;
    }

    @Override
    public int getReviewsCountByNetInfoId(long netInfo_id) {
        String sql = "select count(*) as totalCount from netInfoReview as c inner join netInfo"
                + " on c.netId = netInfo.netId where netInfo.id=?";

        Session session = null;
        try {
            session = getSession();
            Query query = session.createSQLQuery(sql).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
            query.setLong(0, netInfo_id);
            List<?> list = query.list();
            if (list != null && list.size() > 0) {
                if (list.get(0) != null) {
                    @SuppressWarnings("rawtypes")
                    Map map = (Map) list.get(0);
                    return Integer.valueOf(map.get("totalCount").toString());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null)
                releaseSession(session);
        }

        return 0;
    }

}